package io.afu.baseframework.utils;

import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.net.URI;
import java.util.Map;

/**
 * @author RffanLAB.方露宇
 * @version 0.1
 */
@Slf4j
public class WebsocketClient extends WebSocketClient {

    //客户端标识
    private String clientName;
    //客户端连接状态
    private boolean isConnect = false;
    //spring包下的线程池类

    public WebsocketClient(URI serverUri, Map<String, String> httpHeaders,
                               String clientName) {
        super(serverUri, new Draft_6455(), httpHeaders, 0);
        this.clientName = clientName;
    }

    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        log.info(serverHandshake.getHttpStatusMessage());
    }

    @Override
    public void onMessage(String s) {
        log.info("返回的数据为："+s);
    }
    /***检测到连接关闭之后，会更新连接状态以及尝试重新连接***/
    @Override
    public void onClose(int i, String s, boolean b) {
        log.info("------ {} onClose ------{}", clientName, b);
        setConnectState(false);
    }
    /***检测到错误，更新连接状态***/
    @Override
    public void onError(Exception e) {
        log.info("------ {} onError ------{}", clientName, e);
        setConnectState(false);
    }

    public void setConnectState(boolean isConnect) {
        this.isConnect = isConnect;
    }

    public boolean getConnectState(){
        return this.isConnect;
    }




}
